<html>
<head><meta charset="utf-8"><title>How do I print a backslash without escaping it? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20do.20I.20print.20a.20backslash.20without.20escaping.20it.3F.html">How do I print a backslash without escaping it?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="229340907"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20do%20I%20print%20a%20backslash%20without%20escaping%20it%3F/near/229340907" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20do.20I.20print.20a.20backslash.20without.20escaping.20it.3F.html#229340907">(Mar 08 2021 at 17:31)</a>:</h4>
<p>I have this code:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="fm">format!</span><span class="p">(</span><span class="s">"{}{}"</span><span class="p">,</span><span class="w"> </span><span class="n">doc_line</span><span class="p">,</span><span class="w"> </span><span class="s">"</span><span class="se">\\</span><span class="s">n"</span><span class="p">);</span><span class="w"></span>
</code></pre></div>
<p>which generates <code>.. info severity.\\nThe warnings ...</code>. But that's not what I want - I want it to generate a literal backslash, not two backslashes in a row.</p>



<a name="229341213"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20do%20I%20print%20a%20backslash%20without%20escaping%20it%3F/near/229341213" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20do.20I.20print.20a.20backslash.20without.20escaping.20it.3F.html#229341213">(Mar 08 2021 at 17:33)</a>:</h4>
<p>How are you printing the string you created via format?</p>



<a name="229341288"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20do%20I%20print%20a%20backslash%20without%20escaping%20it%3F/near/229341288" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20do.20I.20print.20a.20backslash.20without.20escaping.20it.3F.html#229341288">(Mar 08 2021 at 17:34)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{}{}"</span><span class="p">,</span><span class="w"> </span><span class="s">"doc_line"</span><span class="p">,</span><span class="w"> </span><span class="s">"</span><span class="se">\\</span><span class="s">n"</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<div class="codehilite"><pre><span></span><code>doc_line\n
</code></pre></div>



<a name="229341341"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20do%20I%20print%20a%20backslash%20without%20escaping%20it%3F/near/229341341" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20do.20I.20print.20a.20backslash.20without.20escaping.20it.3F.html#229341341">(Mar 08 2021 at 17:34)</a>:</h4>
<p>my gut says you are debug-formatting the string:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="fm">format!</span><span class="p">(</span><span class="s">"{}{}"</span><span class="p">,</span><span class="w"> </span><span class="s">"doc_line"</span><span class="p">,</span><span class="w"> </span><span class="s">"</span><span class="se">\\</span><span class="s">n"</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{:?}"</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<div class="codehilite"><pre><span></span><code>&quot;doc_line\\n&quot;
</code></pre></div>



<a name="229365988"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20do%20I%20print%20a%20backslash%20without%20escaping%20it%3F/near/229365988" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20do.20I.20print.20a.20backslash.20without.20escaping.20it.3F.html#229365988">(Mar 08 2021 at 20:10)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116155">Jake Goulding</span> <a href="#narrow/stream/122651-general/topic/How.20do.20I.20print.20a.20backslash.20without.20escaping.20it.3F/near/229341213">said</a>:</p>
<blockquote>
<p>How are you printing the string you created via format?</p>
</blockquote>
<p>this was absolutely it, it was being serialized with <code>serde_json::json!</code></p>



<a name="229366015"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20do%20I%20print%20a%20backslash%20without%20escaping%20it%3F/near/229366015" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20do.20I.20print.20a.20backslash.20without.20escaping.20it.3F.html#229366015">(Mar 08 2021 at 20:10)</a>:</h4>
<p>let me try an actual newline and see if that works properly</p>



<a name="229366061"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20do%20I%20print%20a%20backslash%20without%20escaping%20it%3F/near/229366061" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20do.20I.20print.20a.20backslash.20without.20escaping.20it.3F.html#229366061">(Mar 08 2021 at 20:10)</a>:</h4>
<p>yup that fixed it <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<a name="229366066"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20do%20I%20print%20a%20backslash%20without%20escaping%20it%3F/near/229366066" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20do.20I.20print.20a.20backslash.20without.20escaping.20it.3F.html#229366066">(Mar 08 2021 at 20:10)</a>:</h4>
<p>thank you!</p>



<a name="229387735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20do%20I%20print%20a%20backslash%20without%20escaping%20it%3F/near/229387735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20do.20I.20print.20a.20backslash.20without.20escaping.20it.3F.html#229387735">(Mar 08 2021 at 22:28)</a>:</h4>
<p>fwiw, <code>r"\n"</code> is another way to <em>write</em> that without escaping</p>



<a name="229387754"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/How%20do%20I%20print%20a%20backslash%20without%20escaping%20it%3F/near/229387754" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/How.20do.20I.20print.20a.20backslash.20without.20escaping.20it.3F.html#229387754">(Mar 08 2021 at 22:28)</a>:</h4>
<p>(doesn't change the printing side)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>